$(document).ready(function() {
  require(["inputdrop", "tree"], function(inputdrop, tree) {
    var current_selected_node_id = "";

    var key;
    $("input[placeholder]").placeholder();

    function view_init() {
      $("#content_div").css("padding", "0");
      $("#pg-content").css("padding", "0");
    }

  
    // 移动终端指令库表格配置
    var list_col = [
      { text: "", name: "t", width: 3, hideSearch: "hide" },
      { text: "指令名称", name: "name", align: "left", width: 40, tip: true },
      { text: "指令说明", name: "name", align: "left", width: 40, tip: true }
    ];
    var list_oper = [
      { icon: "rh-icon rh-edit", text: "编辑", aclick: open_directive_class_dialog }
    ];
    var configUrl = "api/property/type/list";
    function terminal_directive_list(option) {
      g_grid.render($("#table_div1"), {
        header: list_col,
        url: configUrl,
        type: "xd",
        dataKey: "list",
        oper: list_oper,
        operWidth: "60px",
        paramObj: option.paramObj,
        queryBefore: function(paramObj) {
          if (current_selected_node_id != -1) {
            paramObj.categoryId = current_selected_node_id;
          }
        },
        isLoad: option.isLoad,
        maskObj: option.maskObj,
        hasBorder: false,
        hideSearch: true,
        autoHeight: true,
        showCount: true,
        allowCheckBox: false
      });
    }

    function open_directive_class_dialog(rowData) {
      // 弹出修改框
      $.ajax({
        type: "GET",
        url: "module/base_resource_mant/terminal_event_tpl.html",
        success: function(data) {
          g_dialog.dialog($(data).find("[id=terminal_directive_lib_edit_tpl]"), {
            width: "550px",
            init: init,
            initAfter: initAfter,
            title: "移动终端指令",
            saveclick: save_click
          });
        }
      });
      function init(el) {
        // g_validate.init(el);
        if (rowData) {
         
        }
      }
      function initAfter(el, saveObj) {

        function isJSON(str) {
          if (typeof str == 'string') {
              try {
                  JSON.parse(str);
                  return true;
              } catch(e) {
                  console.log(e);
                  return false;
              }
          }
          console.log('It is not a string!')    
      }
      //  console.log( isJSON($("#directiveOPtions").val()))
       $("#directiveOPtions").blur(function(){
        //  alert(1)
        // var obj= ($("#directiveOPtions").val())
        // var obj2= JSON.parse($("#directiveOPtions").val())
        console.log( isJSON($("#directiveOPtions").val()))
        
        if(isJSON($("#directiveOPtions").val())){
          var options=JSON.parse($("#directiveOPtions").val());
        }else{
          change_error_style($("#directiveOPtions"), "add");
          change_tip($("#directiveOPtions"), null, null, "add", "参数格式不正确");
        }
       })
      }
      function save_click(el, saveObj) {
      
        校验等级定义是否已填
        if (validateForm($("#add_asset_class"))) {
          
          // xd_ajax_post({
          //   url: "api/property/category/add",
          //   paramObj: saveObj,
          //   maskObj: el,
          //   successCallBack: function() {
          //     // 关闭对话框
          //     g_dialog.hide(el);
          //     // 弹出成功提示
          //     g_dialog.operateAlert();
          //     // 重新刷新列表
          //     terminal_directive_list({
          //       paramObj: saveObj,
          //       isLoad: false
          //     });
          //   }
          // });
        }
      }
    }
    terminal_directive_list({
      paramObj: { pageSize: 50 },
      isLoad: true,
      maskObj: "body"
    });
  });
});
